package com.idheima.lon;

import com.idheima.zuoye.zuoye6.Depart;

import java.util.ArrayList;
import java.util.Scanner;

public class Yewu {
    /*需求：实现登录功能，登录界面
        1.定义一个集合保存一组账户和密码
        2.提示用户输入账户和密码
        3.判断账户和密码是否都正确
        4.如果都正确，则登录成功
        5.如果账户或密码错误，则提示用户重新输入,并告知还有几次机会
        判断三次如果都错误就直接结束当前方法
     */
    //管理员对象
    static ArrayList<GuanliClass> guanliList = new ArrayList<>();

    //班级对象信息
    static {
        guanliList.add(new GuanliClass("91传媒初级", 9110, "09:00", "10:00", "杨先生"));
        guanliList.add(new GuanliClass("91传媒进阶", 9111, "10:00", "12:00", "仇先生"));

    }

    //学员对象
    ArrayList<Students> studentsList = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    //管理员信息
    Student student = new Student("ab", "1234");
    //部门对象
    static ArrayList<Depart> departList = new ArrayList<>();

    static {
        departList.add(new Depart("学工部", "人力资源"));
        departList.add(new Depart("管理部", "人力资源"));
    }

    public void login() {
        //登录系统验证用户名以及账号密码
        for (int i = 0; i < 3; i++) {
            System.out.println("==欢迎进入tlias智能学习辅助系统");
            System.out.println("请输入管理员账户：");
            String name = sc.next();
            System.out.println("请输入管理员密码：");
            String password = sc.next();
            if (!student.getName().equals(name) || !student.getPassword().equals(password)) {
                System.out.println("登录失败,账户或密码错误，请重新输入,您还剩" + (2 - i) + "次机会");
                continue;
                //TODO
            } else {
                System.out.println(name + "登录成功");
                jiemian();

            }

        }


    }

    public void jiemian() {

        while (true) {
            System.out.println("1.班级学院管理：");
            System.out.println("2.系统信息管理：");
            System.out.println("3.数据统计管理：");
            System.out.println("4.修改密码：");
            System.out.println("5.退出系统：");
            System.out.println("请输入您的选择");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.班级学院管理：");
                    manage();
                    break;
                case 2:
                    System.out.println("2.系统信息管理：");
                    system();
                    break;
                case 3:
                    System.out.println("3.数据统计管理：");
                    break;
                case 4:
                    System.out.println("4.修改密码：");
                    modifyPassword();
                    break;
                case 5:
                    System.out.println("退出系统：");
                    System.out.println("欢迎您的使用");
                    return;
            }
        }
    }

    public void system() {
        while (true) {
            System.out.println("===系统信息管理===");
            System.out.println("1.部门管理");
            System.out.println("2.员工管理");
            System.out.println("请输入您的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.部门管理");
                    depart();
                    break;
                case 2:
                    System.out.println("2.员工管理");
                    break;
                case 3:
                    System.out.println("3.返回上一级菜单");
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }

    }

    public void depart() {
        while (true) {
            System.out.println("==部门管理==");
            System.out.println("1.新增部门");
            System.out.println("2.修改部门");
            System.out.println("3.删除部门");
            System.out.println("4.查询部门");
            System.out.println("5.返回上一级菜单");
            System.out.println("请输入您的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.新增部门");
                    departadd();
                    break;
                case 2:
                    System.out.println("2.修改部门");

                    break;
                case 3:
                    System.out.println("3.删除部门");

                    break;
                case 4:
                    System.out.println("4.查看部门");

                    break;
                case 5:
                    System.out.println("5.返回上一级菜单");
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    public void departadd() {
        while (true) {
            String name = "";
            System.out.println("请输入新增的部门：");
            name = sc.next();
            if (view(name) == -1) {
                System.out.println("请输入新增的部门职务");
                String position = sc.next();
                departList.add(new Depart(name, position));
                System.out.println(name +"："+ "添加成功");
               break;
            }else {
                System.out.println("部门已存在");

            }
        }


    }

    public int view(String name) {
        for (int i = 0; i < departList.size(); i++) {
            if (departList.get(i).getName().equals(name)) {
                return i;
            }
        }
        return -1;

    }


    public void modifyPassword() {
        //修改密码
        while (true) {
            System.out.println("请输入原密码：");
            String oldPassword = sc.next();
            if (oldPassword.equals(student.getPassword())) {
                break;
            }
            System.out.println("原密码输入错误");
        }

        while (true) {
            System.out.println("请输入新密码：");
            String newPassword = sc.next();
            System.out.println("请再次输入新密码：");
            String newPassword2 = sc.next();
            if (newPassword.equals(newPassword2)) {
                student.setPassword(newPassword);
                break;
            }
            System.out.println("两次输入的密码不一致,请重新输入");
        }
        System.out.println("修改密码成功");
    }

    public void manage() {
        //班级管理系统

        //班级管理系统
        while (true) {
            System.out.println("==班级学员管理==");
            System.out.println("1.班级管理");
            System.out.println("2.学员管理");
            System.out.println("3.回到主界面");
            System.out.println("请输入你的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("班级管理");
                    Class();
                    break;
                case 2:
                    System.out.println("学员管理");
                    studentsManage();
                    break;
                case 3:
                    System.out.println("回到主界面");
                    return;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }

    }


    public void studentsManage() {
        studentsList.add(new Students("张三", "1", "男", "12345678", "大专", "91传媒初级"));
        studentsList.add(new Students("李四", "2", "女", "12345678", "本科", "91传媒进阶"));
        studentsList.add(new Students("王五", "3", "男", "12345678", "高中", "91传媒初级"));
        while (true) {
            System.out.println("学员管理系统");
            System.out.println("1.增加学员");
            System.out.println("2.修改学员");
            System.out.println("3.删除学员");
            System.out.println("4.查看学员");
            System.out.println("5.违纪扣分");
            System.out.println("6.返回上一级菜单");
            System.out.println("请输入您的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.增加学员");
                    addStudent();
                    break;
                case 2:
                    System.out.println("2.修改学员");
                    modifyStudents();
                    break;
                case 3:
                    System.out.println("3.删除学员");
                    deletestudents();
                    break;
                case 4:
                    System.out.println("4.查看学员");
                    showstudents();
                    break;
                case 5:
                    System.out.println("5.违纪扣分");
                    deduct();
                    break;
                case 6:
                    System.out.println("6.返回上一级菜单");
                    return;
                default:
                    System.out.println("请输入正确的选项");
            }
        }

    }

    public void deduct() {
        System.out.println("请输入想要扣分的学生的学号");
        String id = sc.next();
        int index = chaid(id);
        if (index == -1) {
            System.out.println("没有此学号");
        } else {
            System.out.println("请输入本次所扣分数");
            Double deduct = sc.nextDouble();
            studentsList.get(index).setDeduct(studentsList.get(index).getDeduct() + deduct);
            studentsList.get(index).setTimes(studentsList.get(index).getTimes() + 1);
            System.out.println("扣分成功");
            System.out.println("学号：" + studentsList.get(index).getId() + "  姓名：" + studentsList.get(index).getName()
                    + "," + "本次扣分：" + studentsList.get(index).getDeduct() + "," + "累计扣分次数：" + studentsList.get(index).getTimes() + "," + "累计扣分：" + studentsList.get(index).getDeduct());
        }

    }

    public void showstudents() {
        while (true) {
            System.out.println("=====查看学员====");
            System.out.println("1.查看所有学员");
            System.out.println("2.学员名称查询");
            System.out.println("3.学员学号查询");
            System.out.println("4.最高学历查询");
            System.out.println("5.所属班级查询");
            System.out.println("6.返回上一级菜单");
            System.out.println("请输入您的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.查看所有学员");
                    showall();
                    break;
                case 2:
                    System.out.println("2.学员名称查询");
                    showname();
                    break;
                case 3:
                    System.out.println("3.学员学号查询");
                    showid();
                    break;
                case 4:
                    System.out.println("4.最高学历查询");
                    showXueli();
                    break;
                case 5:
                    System.out.println("5.所属班级查询");
                    seletStudentsByClassName();
                    break;
                case 6:
                    System.out.println("返回上一级菜单");
                    return;
                default:
                    System.out.println("请输入正确的选项");
            }
        }
    }

    public void seletStudentsByClassName() {
        System.out.println("请输入要查询的班级名称：");
        String classname = sc.next();
        //此班级学生集合
        ArrayList<Students> studentsList1 = new ArrayList<>();
        for (Students students : studentsList) {
            if (students.getClassname().equals(classname)) {
                studentsList1.add(students);
            }
        }
        if (studentsList1.size() != 0) {
            for (Students students : studentsList1) {
                System.out.printf("%-10s %-10s %-5s %-15s %-10s %-10s %-10s %-20s%n",
                        "姓名", "学号", "性别", "手机号", "学历", "违纪次数", "违纪扣分", "所属班级");
                System.out.printf("%-10s %-10s %-5s %-15s %-10s %-10d %-10.1f %-20s%n",
                        students.getName(), students.getId(), students.getSex(), students.getPhone(), students.getBackground(), students.getTimes(), students.getDeduct(), students.getClassname());
            }
        } else {
            System.out.println("没有此班级的学员信息");
        }

    }

    public void showXueli() {
        System.out.println("请输入要查询的学历：");
        String xueli = sc.next();
        //此学历学生集合
        ArrayList<Students> studentsList1 = new ArrayList<>();
        for (Students students : studentsList) {
            if (students.getBackground().equals(xueli)) {
                studentsList1.add(students);
            }
        }
        if (studentsList1.size() != 0) {
            for (Students students : studentsList1) {
                System.out.printf("%-10s %-10s %-5s %-15s %-10s %-10s %-10s %-20s%n",
                        "姓名", "学号", "性别", "手机号", "学历", "违纪次数", "违纪扣分", "所属班级");
                System.out.printf("%-10s %-10s %-5s %-15s %-10s %-10d %-10.1f %-20s%n",
                        students.getName(), students.getId(), students.getSex(), students.getPhone(), students.getBackground(), students.getTimes(), students.getDeduct(), students.getClassname());
            }
        } else {
            System.out.println("没有此学历的学员信息");
        }


    }

    public void showid() {
        System.out.println("请输入要查询的学员学号:");
        String id = sc.next();
        int index = chaid(id);
        if (index != -1) {
            Students s1 = studentsList.get(index);
            System.out.printf("%-10s %-10s %-5s %-15s %-10s %-20s%n",
                    "姓名", "学号", "性别", "手机号", "学历", "所属班级");
            System.out.printf("%-10s %-10s %-5s %-15s %-10s %-20s%n",
                    s1.getName(), s1.getId(), s1.getSex(), s1.getPhone(), s1.getBackground(), s1.getClassname());
        } else {
            System.out.println("没有此学员");
        }
    }

    public void showall() {
        for (int i = 0; i < studentsList.size(); i++) {
            System.out.println("姓名：" + studentsList.get(i).getName() +
                    "  学号：" + studentsList.get(i).getId() +
                    "  性别：" + studentsList.get(i).getSex() + "  手机号：" + studentsList.get(i).getPhone() + "  学历：" + studentsList.get(i).getBackground() + "  班级：" + studentsList.get(i).getClassname());
        }

    }

    public void showname() {
        System.out.println("请输入要查询的学员名称:");
        String name = sc.next();
        for (Students students : studentsList) {
            if (students.getName().equals(name)) {
                System.out.println("姓名：" + students.getName() + "  学号：" + students.getId() + "  性别：" + students.getSex() + "  手机号：" + students.getPhone() + "  学历：" + students.getBackground() + "  班级：" + students.getClassname());
                break;
            }
        }
        System.out.println("没有此学员");

    }

    public void deletestudents() {
        System.out.println("请选择删除方式:1.根据学号单个删除 /2.根据学号批量删除");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.println("根据学号单个删除");
                dandelete();
                break;
            case 2:
                System.out.println("根据学号批量删除");
                batchdelete();
                break;
            default:
                System.out.println("请输入正确的选项");

        }
    }

    public void dandelete() {
        for (Students students : studentsList) {
            System.out.println("打印前" + students);
        }
        while (true) {
            System.out.println("请输入要删除的学员的学号:");
            String id = sc.next();
            if (chaid(id) != -1) {
                System.out.println("确定删除此学员吗?(y/n)");
                String choice = sc.next();
                if (!choice.equals("y")) {
                    return;
                }
                for (int i = 0; i < studentsList.size(); i++) {
                    if (studentsList.get(i).getId().equals(id)) {
                        studentsList.remove(i);
                    }
                }

                System.out.println("删除成功");
                for (Students students : studentsList) {
                    System.out.println("打印后" + students);
                }
                break;

            } else {
                System.out.println("没有此学号,请重新输入");
            }

        }
    }

    public void batchdelete() {

        //删除前学生list展示
        System.out.println("请输入要删除的学员的学号:");
        String id = sc.next();
        String[] idArray = id.split(",");
        if (idArray.length > 1) {
            System.out.println("确定删除此学员吗?(y/n)");
            String choice = sc.next();
            if (!choice.equals("y")) {
                return;
            }
            for (int i = 0; i < idArray.length; i++) {
                int index = chaid(idArray[i]);
                if (index != -1) {
                    Students students = studentsList.get(index);
                    studentsList.remove(index);
                    System.out.println(students.getId() + "的" + students.getName() + "删除成功");


                } else {
                    System.out.println("学号：" + idArray[i] + "没有此学号，不会删除");
                }
            }
        } else {
            int index = chaid(idArray[0]);
            if (index != -1) {
                Students students = studentsList.get(index);
                studentsList.remove(index);
                System.out.println(students.getId() + "的" + students.getName() + "删除成功");

            } else {
                System.out.println("学号：" + idArray[0] + "没有此学号，不会删除");
            }

        }


    }

    public void modifyStudents() {
        String id = "";
        while (true) {
            System.out.println("请输入要修改的学员的学号:");
            id = sc.next();
            if (chaid(id) != -1) {
                break;
            }
            System.out.println("没有此学号,请重新输入");
        }
        System.out.println("请输入修改后的学员的姓名:");
        String name = sc.next();
        String sex = null;
        while (true) {
            System.out.println("请输入修改后的学员的性别:");
            sex = sc.next();
            if (sex.equals("男") || sex.equals("女")) {
                break;
            }
            System.out.println("请输入正确的性别,请重新输入");
        }

        String phone = null;
        while (true) {
            System.out.println("请输入修改后的学员的手机号:");
            phone = sc.next();
            if (phone.matches("1[3-9]\\d{9}")) {
                break;
            }
            System.out.println("请输入正确的手机号,请重新输入");
        }
        System.out.println("请输入修改后的学员的学历:");
        String background = sc.next();
        System.out.println("请输入修改后的学员的班级:");
        System.out.println("现有班级如下：");
        for (int i = 0; i < guanliList.size(); i++) {
            System.out.println(guanliList.get(i).getName());
        }
        String classname = "";
        while (true) {
            System.out.println("请输入修改后学生的所属班级:");
            classname = sc.next();
            if (classnameCheck(classname)) {
                break;
            } else {
                System.out.println("该班级不存在，请重新输入班级名称");
            }
        }
        Students students = new Students(name, id, sex, phone, background, classname);
        System.out.println("修改信息成功！");


    }

    public void addStudent() {
        String id = "";
        while (true) {
            System.out.println("请输入新增学生的学号:");
            id = sc.next();
            if (chaid(id) == -1) {
                break;
            } else {
                System.out.println("该学号已存在");
            }
        }
        System.out.println("请输入新增学生的姓名:");
        String name = sc.next();
        String sex = null;
        while (true) {
            System.out.println("请输入新增学生的性别:");
            sex = sc.next();
            if (sex.equals("男") || sex.equals("女")) {
                break;
            }
            System.out.println("请输入正确的性别");
        }

        String phone = null;
        while (true) {
            System.out.println("请输入新增学生的手机号:");
            phone = sc.next();
            if (phone.matches("1[3-9]\\d{9}")) {
                break;
            }
            System.out.println("请输入正确的手机号");
        }
        System.out.println("请输入新增学生的学历:");
        String background = sc.next();
        System.out.println("现有班级如下：");
        for (int i = 0; i < guanliList.size(); i++) {
            System.out.println(guanliList.get(i).getName());
        }
        String classname = "";
        while (true) {
            System.out.println("请输入新增学生的所属班级:");
            classname = sc.next();
            if (classnameCheck(classname)) {
                break;
            } else {
                System.out.println("该班级不存在，请重新输入班级名称");
            }
        }

        studentsList.add(new Students(name, id, sex, phone, background, classname));
        System.out.println("新增学员成功！");


    }

    public boolean classnameCheck(String classname) {
        for (int i = 0; i < guanliList.size(); i++) {
            if (guanliList.get(i).getName().equals(classname)) {
                return true;
            }
        }
        return false;
    }

    public int chaid(String id) {
        for (int i = 0; i < studentsList.size(); i++) {
            if (studentsList.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }


    public void Class() {

        while (true) {
            System.out.println("===班级管理===");
            System.out.println("1.新增班级");
            System.out.println("2.修改班级");
            System.out.println("3.删除班级");
            System.out.println("4.查询班级");
            System.out.println("5.返回上一级");
            System.out.println("请输入你的选择：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("新增班级");
                    add();
                    break;
                case 2:
                    System.out.println("修改班级");
                    modify();
                    break;
                case 3:
                    System.out.println("删除班级");
                    delete();
                    break;
                case 4:
                    System.out.println("查看班级");
                    classMenu();
                    break;
                case 5:
                    System.out.println("返回上一级");
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }


    }

    public void add() {
        String name = "";
        int room = 0;
        //添加班级信息
        while (true) {
            System.out.println("请输入新班级名称：");
            name = sc.next();
            if (cha(name) == null) {
                break;
            } else {
                System.out.println("该班级已存在");
            }
        }
        while (true) {
            System.out.println("请输入新班级教室：");
            room = sc.nextInt();
            if (check(room) == null) {
                break;
            } else {
                System.out.println("该教室已存在");
            }
        }

        System.out.println("请输入新班级开课时间：");
        String time = sc.next();
        System.out.println("请输入新班级结课时间：");
        String endtime = sc.next();
        System.out.println("请输入新班级的班主任：");
        String teacher = sc.next();
        GuanliClass guanliListClass = new GuanliClass(name, room, time, endtime, teacher);
        guanliList.add(guanliListClass);
        System.out.println("添加成功");


    }

    public void modify() {
        //修改班级信息
        GuanliClass test;
        while (true) {
            System.out.println("请输入要修改的班级名称：");
            String name = sc.next();
            test = cha(name);
            if (test != null) {
                break;
            }
            System.out.println("没有此班级,请重新输入");
        }

        System.out.println("请输入修改后的班级名称：");
        String newName = sc.next();
        System.out.println("请输入修改后的班级教室：");
        int room = sc.nextInt();
        System.out.println("请输入修改后的班级开课时间：");
        String time = sc.next();
        System.out.println("请输入修改后的班级结课时间：");
        String endtime = sc.next();
        System.out.println("请输入修改后的班级的班主任：");
        String teacher = sc.next();
        test.setName(newName);
        test.setClassroom(room);
        test.setTime(time);
        test.setEndtime(endtime);
        test.setTeacher(teacher);

        System.out.println("修改成功");
    }

    public GuanliClass cha(String name) {
        //查看方法
        for (int i = 0; i < guanliList.size(); i++) {
            if (guanliList.get(i).getName().equals(name))
                return guanliList.get(i);
        }
        return null;
    }

    public GuanliClass check(int room) {
        for (int i = 0; i < guanliList.size(); i++) {
            if (guanliList.get(i).getClassroom() == room)
                return guanliList.get(i);
        }
        return null;
    }

    public void delete() {
        //删除班级信息
        GuanliClass test;
        while (true) {
            System.out.println("请输入要删除的课程名称:");
            String name = sc.next();
            test = cha(name);
            //删除逻辑
            if (test != null) {
                System.out.println("确认要删除此课程吗？y(确认)/n(取消并返回)");
                String choice = sc.next();
                if (!choice.equals("y")) {

                    return;
                }
                guanliList.remove(test);
                System.out.println("删除成功");
                return;
            } else {
                System.out.println("没有此课程请重新输入：");
                System.out.println("----------------------");
            }

        }


    }

    public void showAllClazz() {
        System.out.println("-----班级信息如下：-------");
        for (GuanliClass guanliListClass : guanliList) {
            System.out.println("班级：" + guanliListClass.getName());
            System.out.println("教室：" + guanliListClass.getClassroom());
            System.out.println("开课时间：" + guanliListClass.getTime());
            System.out.println("结束时间：" + guanliListClass.getEndtime());
            System.out.println("任课老师：" + guanliListClass.getTeacher());
        }
    }

    public void classMenu() {
        System.out.println("==班级菜单===");
        System.out.println("1.查看所有班级");
        System.out.println("2.根据班级名称查询");
        System.out.println("3.根据结课时间查询");
        System.out.println("4.返回上一级");
        System.out.println("请输入您的选择：");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.println("1.查看所有班级");
                showAllClazz();
                break;
            case 2:
                System.out.println("2.根据班级名称查询");
                nameCheck();
                break;
            case 3:
                System.out.println("3.根据结课时间查询");
                endTimeCheck();
                break;
            case 4:
                System.out.println("4.返回上一级");
                return;
            default:
                System.out.println("输入错误");
                break;
        }


    }

    public void nameCheck() {
        while (true) {
            System.out.println("请输入要查询的班级名称");
            String name = sc.next();
            for (int i = 0; i < guanliList.size(); i++) {
                if (guanliList.get(i).getName().equals(name)) {
                    System.out.println(guanliList.get(i));
                    return;
                }
            }
            System.out.println("没有此班级");
        }
    }

    public void endTimeCheck() {
        while (true) {
            System.out.println("请输入要查询的结课时间");
            String endtime = sc.next();
            for (int i = 0; i < guanliList.size(); i++) {
                if (guanliList.get(i).getEndtime().equals(endtime)) {
                    System.out.println(guanliList.get(i));
                    return;
                }
            }
            System.out.println("没有查询到");
        }
    }
}
